<?php

/**
 * @author    1054229380@qq.com
 * @since    2017-7-1
 * */
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
    'http://192.168.2.188:8080',
    'http://192.168.2.199:8080',
    'http://192.168.2.189:8080',
    'http://192.168.2.188:90',
    'http://192.168.2.189:8081',
    'http://localhost:8080',
    'http://sml.lfmall.net'
);
if (in_array($origin, $allow_origin)) {
    header('Access-Control-Allow-Origin:' . $origin);
    header('Access-Control-Allow-Methods:POST');
    header("Access-Control-Allow-Credentials: true");
    header('Access-Control-Allow-Headers:x-requested-with,content-type');
}
require 'include/common.inc.php';
//print_r($_SERVER);
$router = new router();

$_url = $router->getURL();
$_tmpArr = explode('/', $_url);
//e($_tmpArr);
$_clsLocation = 1;
$_adminUrl = str_replace('/', '', _ADMINURL);
$_controlClassDirTmp = $_controlClass = $_method = $_para = null;
if (isset($_tmpArr[1])) {
    if ($_tmpArr[1] == "api" || $_tmpArr[1] == "weixin" || $_tmpArr[1] == $_adminUrl) {
        $_clsLocation = $_clsLocation + 1;
        $_controlClassDir = $_tmpArr[1];
        $_controlClassDirTmp = $_controlClassDir . "/";
        $router->classDirNum = 1;
    }
    $_controlClass = isset($_tmpArr[$_clsLocation]) ? $_tmpArr[$_clsLocation] : "";
    if ($_tmpArr[1] == $_adminUrl && $_controlClass == "") {
        $_controlClass = "sysUser";
    }
    if ($_tmpArr[1] == "weixin" && $_controlClass == "") {
        $_controlClass = "index";
    }
}
if (isset($_tmpArr[$_clsLocation + 1])) {
    $_method = $_tmpArr[$_clsLocation + 1];
}
if (isset($_tmpArr[$_clsLocation + 2])) {
    $_para = $_controlClass;
    for ($i = 3; $i < count($_tmpArr); $i++) {
        $_para .= ">" . $_tmpArr[$i];
    }
}
if ($_controlClass == "") {
    $_controlClass = "server";
}
if ($_method == "") {
    $_method = "getArray";
}
$filename = APP_ROOT . '/api/' . $_controlClassDirTmp . $_controlClass . '.php';

define('CONTROLLER', $_controlClass);
define('ACTION', $_method);
define('__SELF__', _SURL . '/index.php/' . CONTROLLER . '/' . ACTION);
define('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
define('IS_GET', REQUEST_METHOD == 'GET' ? true : false);
define('IS_POST', REQUEST_METHOD == 'POST' ? true : false);

if (!file_exists($filename)) {
    exit("文件不存在!");
} else {
    require $filename;
    $myclass = $_controlClass;
    $objControl = new $myclass();
    $objControl->$_method($_para);
}
?>